require "jdbc_migration"
require "jdbc_migration/meta_data"
require "jdbc_migration/rails_migrator"
module JdbcMigration
  module Base
    def migrate_from_jdbc(source_db, catalog, schema_pattern, table_name_pattern, options = nil)
      from_jdbc(source_db, catalog, schema_pattern, table_name_pattern, options) do |migrator, meta_data, tables|
        migrator.process_schema(*tables)
        migrator.process_data(*tables)
      end
    end

    def migrate_schema_from_jdbc(source_db, catalog, schema_pattern, table_name_pattern, options = nil)
      from_jdbc(source_db, catalog, schema_pattern, table_name_pattern, options) do |migrator, meta_data, tables|
        migrator.process_schema(*tables)
      end
    end

    def migrate_data_from_jdbc(source_db, catalog, schema_pattern, table_name_pattern, options = nil)
      from_jdbc(source_db, catalog, schema_pattern, table_name_pattern, options) do |migrator, meta_data, tables|
        migrator.process_data(*tables)
      end
    end

    def from_jdbc(source_db, catalog = nil, schema_pattern = "PUBLIC", table_name_pattern = nil, options = nil, &block)
      migrator  = JdbcMigration::RailsMigrator.new(self, options)
      meta_data = JdbcMigration::MetaData.create(source_db)
      tables = meta_data.tables(catalog, schema_pattern, table_name_pattern)
      yield(migrator, meta_data, tables)
    end
  end
end
